<template>
	<div class="padding20">
		<loading :show="parentShow">
			<el-table
				:data="tableData"
				style="width: 100%; margin-bottom: 20px"
				row-key="id"
				border
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
			>
				<el-table-column type="selection" width="55" show-overflow-tooltip></el-table-column>
				<el-table-column prop="name" label="名称" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column prop="isAdmin" label="是否超管" :show-overflow-tooltip="true" :formatter="isAdmFmt"></el-table-column>
				<el-table-column prop="status" label="状态" :show-overflow-tooltip="true">
					<template slot-scope="scope">
						<el-tag type="success" size="mini" v-if="scope.row.status == 1">已启用</el-tag>
						<el-tag type="danger" size="mini" v-else>未启用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" min-width="180">
					<template slot-scope="scope">
						<el-button
							v-if="$tools.hasPermission('cms:cmsRoleChannel:save') && scope.row.isAdmin == 2"
							size="mini"
							@click="handleMenuAuthEdit(scope.row.id)"
							plain
						>
							栏目权限
						</el-button>
					</template>
				</el-table-column>
			</el-table>
		</loading>
		<pagination :total="page.total" :current-page="page.current" :page-size="page.size" @refreshData="queryList"></pagination>
		<cmsrolechannel-edit ref="menuauthEdit"></cmsrolechannel-edit>
	</div>
</template>
<script>
import { rolePage } from "@/api/auth/sysrole.js"
import FortressPagination from "@/components/fortress-pagination.vue"
import cmsrolechannelEdit from "./cmsrolechannel-edit.vue"

export default {
	data() {
		return {
			parentShow: "",
			tableData: [],
			page: {
				current: 1,
				size: 10,
				total: 0,
			},
		}
	},
	components: {
		pagination: FortressPagination,
		cmsrolechannelEdit,
	},
	created() {
		this.tableData = []
		this.queryList()
	},
	methods: {
		//查询表格数据
		queryList(page) {
			this.parentShow = true
			if (page) {
				this.page.current = page.current ? page.current : this.page.current
				this.page.size = page.size ? page.size : this.page.size
			}
			rolePage(this.page).then(resp => {
				if (resp.code == 0) {
					setTimeout(() => {
						this.parentShow = false
						this.tableData = resp.data.records
						this.page.total = resp.data.total
						//查询页大于总页数，重新查询
						let cu =
							this.page.total % this.page.size == 0
								? parseInt(this.page.total / this.page.size)
								: parseInt(this.page.total / this.page.size + 1)
						if (cu > 0 && this.page.current > cu) {
							this.queryList({ current: cu })
						}
					}, 600)
				} else {
					this.parentShow = false
					this.$msg({
						message: resp.msg,
						type: "error",
					})
				}
			})
		},

		//是否超管格式化返回
		isAdmFmt(row) {
			let typeName = "否"
			if (row.isAdmin == 1) {
				typeName = "是"
			}
			return typeName
		},
		//状态格式化返回
		statusFmt(row) {
			let typeName = "未启用"
			if (row.status == 1) {
				typeName = "已启用"
			}
			return typeName
		},
		//菜单权限
		handleMenuAuthEdit(id) {
			this.menuAuthVisible = true
			this.$nextTick(() => {
				this.$refs.menuauthEdit.init(id)
			})
		},
	},
}
</script>
<style scoped></style>
